﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class LopHocProvider
    {
        public static List<LopHoc> LayDanhSachLopHoc()
        {
            var lst = new List<LopHoc>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.LopHocs select u);

                foreach (var row in query)
                {
                    var lh = new LopHoc();
                    lh.MaLop = row.MaLop;
                    lh.MaGV = row.MaGV;
                    lh.MaKH = row.MaKH;
                    lh.TenLop = row.TenLop;
                    lh.SiSo = row.SiSo;

                    lst.Add(lh);
                }
            }

            return lst;
        }

        public static int ThemLopHoc(LopHoc lh)
        {
            int maLH = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.LopHocs.Add(lh);
                    context.SaveChanges();
                    maLH = lh.MaLop;
                }
            }
            catch
            {

            }
            return maLH;
        }

        public static bool XoaLopHoc(int maLH)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    //var lh = context.LopHocs.Single(m => m.MaLH.Equals(maLH));

                    //context.KhoaHocsLopHocs.Remove(lh);
                    //context.SaveChanges();

                    var query = (from u in context.LopHocs
                                 where u.MaLop.Equals(maLH)
                                 select u).First();

                    context.LopHocs.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool CapNhatLopHoc(LopHoc lh)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.LopHocs
                                 where u.MaLop.Equals(lh.MaLop)
                                 select u).First();
                    query.MaGV = lh.MaGV;
                    query.MaKH = lh.MaKH;
                    query.TenLop = lh.TenLop;
                    query.SiSo = lh.SiSo;

                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
